﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace QuanLyPhongKham
{
    public partial class frmBangKeThuTien : BaseForm
    {
        private DAL.AccessData m;
        private string user = "", sql = "";
        private DataSet ds = new DataSet();

        public frmBangKeThuTien(DAL.AccessData acc)
        {
            this.m = acc;
            InitializeComponent();
        }

        private void frmBangkethutien_Load(object sender, EventArgs e)
        {
            user = m.user;
            cboPhongkham.DisplayMember = "TENKP";
            cboPhongkham.ValueMember = "MAKP";
            cboPhongkham.DataSource = m.get_data("select makp,tenkp from " + user + ".btdkp").Tables[0];
            cboBacsi.DisplayMember = "HOTEN";
            cboBacsi.ValueMember = "MA";
            cboBacsi.DataSource = m.get_data("select ma,hoten from " + user + ".dmbs").Tables[0];
        }

        private void butIn_Click(object sender, EventArgs e)
        {
            sql = "select a.id,a.sobienlai,a.mabn,c.hoten,c.namsinh,d.hoten as tenbs,sum(to_number(decode(b.mavp,-9999,b.soluong*b.dongia,0))) as sotien, sum(to_number(decode(b.mavp,-9998,b.soluong*b.dongia,0))) as tienthuoc, sum(case when b.mavp not in(-9998,-9999) then b.soluong*b.dongia else 0 end) as tienpttt from " + user + ".v_vienphill a," + user + ".v_vienphict b," + user + ".btdbn c," + user + ".dmbs d ";
            sql += "where a.id=b.id and a.mabn=c.mabn and a.mabs=d.ma and to_date(to_char(a.ngay,'dd/mm/yyyy'),'dd/mm/yyyy') between to_date('" + tungay.Text + "','dd/mm/yyyy') and to_date('" + denngay.Text + "','dd/mm/yyyy')";
            if (cboBacsi.SelectedIndex != -1) sql += " and a.mabs='" + cboBacsi.SelectedValue.ToString() + "'";
            if (cboPhongkham.SelectedIndex != -1) sql += " and a.makp='" + cboPhongkham.SelectedValue.ToString() + "'";
            sql += " group by a.id,a.sobienlai,a.mabn,c.hoten,c.namsinh,d.hoten order by a.sobienlai";
            ds = m.get_data(sql);
            ds.WriteXml(@"..\v_bangke.xml", XmlWriteMode.WriteSchema);
            string tenfile = "";
            tenfile = (tungay.Text == denngay.Text) ? "Ngày " + tungay.Text : "Từ ngày " + tungay.Text + " đến ngày " + denngay.Text;
            if (ds.Tables.Count == 0 || ds.Tables[0].Rows.Count == 0)
            {
                MessageBox.Show("Không có số liệu!",DAL.AccessData.Msg);
                return;
            }
            
            frmReport f = new frmReport(m, ds.Tables[0], "", "v_bangke.rpt", tenfile, "", "", "", "", "", "", "", "", "", "", "");
            this.FormMain.OpenTab(f);
            //f.ShowDialog();
        }

        private void butKetthuc_Click(object sender, EventArgs e)
        {
            //this.Close();
        }
    }
}